package com.fs.ssm004.controller;

import com.fs.ssm004.pojo.Course;
import com.fs.ssm004.service.CourseService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @RequestMapping("/getview1")
    public String getListView() {
        return "coursemanage";
    }

    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String, Object> getList() {
        List<Course> courseSet = courseService.getAll();
        Map<String, Object> result = new HashMap<>();
        System.out.println(courseSet);
        result.put("code", 0);
        result.put("message", "获取课程数据成功");
        result.put("courseList",courseSet);
        return result;
    }

    @RequestMapping("/getListByPage")
    @ResponseBody
    public Map<String, Object> getListByPage(Integer currentPage,
                                             Integer pageSize) {

        PageInfo<Course> pageInfo = courseService.getUsersByPage(currentPage, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取课程数据成功");
        result.put("courseList", pageInfo.getList()); // 返回查询出来的分页数据
        result.put("totalPage", pageInfo.getPages()); //返回当前总共有多少页
        return result;
    }
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String, Object> getone(Integer c_id){
        Map<String, Object> result = new HashMap<>();
        Course course = courseService.getoneByCourseId(c_id);
        if (course == null) {
            result.put("code", -1);
            result.put("message", "获取用户数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "获取用户数据成功");
            result.put("course", course);
        }
        return result;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(Course course) {
        System.out.println(course);
        Map<String, Object> result = new HashMap<>();
        boolean modified;
        if (course.getC_id()>0) {
            modified = courseService.modifyOne(course);
        } else {
            modified = courseService.addOne(course);
        }
        if (!modified) {
            result.put("code", -1);
            result.put("message", "编辑课程数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "编辑课程数据成功");
        }
        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Course course) {
        Map<String, Object> result = new HashMap<>();
        boolean deleted = courseService.removeOne(course);
        if (!deleted) {
            result.put("code", -1);
            result.put("message", "删除课程数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "删除课程数据成功");
        }
        return result;
    }
    @RequestMapping("/search")
    @ResponseBody
    public Map<String, Object> search(String classname) {
        Map<String, Object> result = new HashMap<>();
        List<Course> courseList = courseService.getCourseThatNameLike(classname);
        if (courseList == null) {
            result.put("code", -1);
            result.put("message", "查询用户数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "查询用户数据成功");
            result.put("user", courseList);
        }
        System.out.println(courseList);
        return result;
    }
}
